<template>
	<view>
		<view class="example">
			<!-- 基础用法，不包含校验规则 -->
			<uni-forms class="form" ref="baseForm" :modelValue="baseFormData" border label-position="top">
				<uni-forms-item label="标题" name="title" required>
					<uni-easyinput v-model="baseFormData.forumTitle" placeholder="帖子标题" />
				</uni-forms-item>
				<uni-forms-item label="内容" :border="true" required name="txt">
					<uni-easyinput type="textarea" autoHeight v-model="baseFormData.forumMessage" placeholder="帖子内容" />
				</uni-forms-item>
				<button type="primary" @click="submit()">提交</button>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	import store from '@/store/index.js'
	import {
		mapState,
		mapMutations,
		mapActions,
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				baseFormData: {
					userId: 0,
					forumTitle: '',
					forumMessage: '',
					forumCreatTime: '',
					table: 'forum',
				}

			}
		},
		methods: {
			...mapMutations('course', ['getForumData']),
			getTime() {
				// 获取当前时间的完整信息
				var now = new Date();
				// 格式化输出精确到秒·····2025-01-31 00:13:58
				var formattedTime = now.getFullYear() + '-' +
					('0' + (now.getMonth() + 1)).slice(-2) + '-' +
					('0' + now.getDate()).slice(-2) + ' ' +
					('0' + now.getHours()).slice(-2) + ':' +
					('0' + now.getMinutes()).slice(-2) + ':' +
					('0' + now.getSeconds()).slice(-2);

				// console.log(formattedTime);
				return formattedTime
			},
			//提交数据
			submit(ref) {
				//获取用户id
				this.baseFormData.userId = this.user.userId
				//获取时间
				this.baseFormData.forumCreatTime = this.getTime()
				console.log(JSON.stringify(this.baseFormData))
				//检验有必要的信息有没有填写
				if (Object.values(this.baseFormData).every(item => {
						// console.log(item)
						return item
					})) {
					//数据发给后端
					uni.request({
						url: 'http://localhost:3000/course',
						method: 'post',
						data: this.baseFormData,
						success: (res) => {
							console.log("res:", res)
							uni.showToast({
								icon: 'success',
								title: '成功发布',
								duration: 500
							})
							this.getForumData()
							setTimeout(() => {
								console.log(3)
								uni.navigateBack()
							}, 500)
						}
					});
				} else {
					uni.showToast({
						icon: 'error',
						title: '有些区域未填值',
						duration: 1000
					})
				}
			},
		},
		computed: {
			...mapState('user', ['user']),
		}

	}
</script>

<style scoped lang="scss">
	.example {
		padding: 15px;
		background-color: #fff;

		.form {
			.images {
				width: 200rpx;
			}
		}
	}
</style>